<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Myemail {
	
	public function sendcheck(){
		
		$CI =& get_instance();		
		$CI->load->model('email_model','em');
		$checkinfo = $CI->em->get_es_list();
		
		if($checkinfo){
			
			foreach ($checkinfo as $k=>$v){

				$strings = $CI->em->temp_e_list($v->tempid);
				
				if($strings){

					$temp = json_decode($v->tempinfo);
					
					$et = true;

					foreach ($temp as $key=>$val){
						$data1[] = '$'.$key;
						$data2[] = $val;
						$et = $et&&$val;
					}
					
					if(!$et) continue;
					
					$sendinfo = str_replace($data1,$data2,$strings->tempinfo);
					$subject = str_replace($data1,$data2,$strings->temptitle);
					
					$conf = json_decode($v->confinfo);
					
					$ec = true;
					
					foreach ($conf as $key=>$val){
						$data1[] = '$'.$key;
						$data2[] = $val;
						$ec = $ec&&$val;
					}
						
					if(!$ec) continue;
					
					if($this->SendMail($subject,$sendinfo,$conf)) $CI->em->update_emailstatus($v->id);
				}
			}	

		}else{
			
			echo "have no email to send!";
			
		}		
	}
	
	
	public function SendMail($subject,$message, $conf){
		
		//配置邮箱参数
		$config = array();
		$config['protocol']= 'smtp';
	
		$config['smtp_host'] = $conf->smtpserver;
		$config['smtp_user'] = $conf->smtpuser;
		$config['smtp_pass'] = $conf->smtppassword;
		$config['smtp_port'] = '465';
	
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;
		$config['mailtype'] = 'html';
		$config['newline'] = "\r\n";
		$config['crlf'] = "\r\n";
	
		$sdk =& get_instance();
		$sdk->load->library('email',$config);
	
		//开始发送邮件
		$sdk->email->clear();
	
		$sdk->email->to($conf->email);
		$sdk->email->from($conf->smtpuser);
		$sdk->email->subject($subject);
		$sdk->email->message($message);
		$status = $sdk->email->send();

		echo $sdk->email->print_debugger();
		return $status;

		
	}
	
}